package com.firefly.admin.common.config;

import com.firefly.admin.common.properties.Knife4jProperties;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private final OpenApiExtensionResolver openApiExtensionResolver;
    @Autowired
    private Knife4jProperties knife4jProperties;

    @Autowired
    public SwaggerConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean(value = "compactApi")
    public Docket compactApi() {
        String groupName = knife4jProperties.getConfig().getGroupName();
        return new Docket(DocumentationType.SWAGGER_2)
                .host(knife4jProperties.getConfig().getHost())
                .apiInfo(getApiInfo())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(knife4jProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName))
                .globalRequestParameters(getParameterList());
    }

    private ApiInfo getApiInfo() {
        Contact contact = new Contact(knife4jProperties.getContact().getName(), knife4jProperties.getContact().getUrl(), knife4jProperties.getContact().getEmail());
        return new ApiInfoBuilder()
                .title(knife4jProperties.getApiInfo().getTitle())
                .description(knife4jProperties.getApiInfo().getDescription())
                .termsOfServiceUrl(knife4jProperties.getApiInfo().getTermsServiceUrl())
                .contact(contact)
                .version(knife4jProperties.getApiInfo().getVersion())
                .build();
    }

    public List<RequestParameter> getParameterList() {
        List<RequestParameter> parameterList = new ArrayList<>();
        RequestParameterBuilder requestParameterBuilder = new RequestParameterBuilder();
        requestParameterBuilder.name("Authorization").description("认证Token").in(ParameterType.HEADER).required(false).build();
        parameterList.add(requestParameterBuilder.build());
        return parameterList;
    }
}
